package com.hundsun.s3.client.web.action;

import com.hundsun.s3.common.dto.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用于操作用户各种信息的action
 * @author chenji23607
 *
 */
@Controller
public class ClientInfoAction {

    private static final Logger LOG = LoggerFactory.getLogger(ClientInfoAction.class);

    @RequestMapping(value="/info.htm")
    public void login(ModelMap model, HttpSession session, HttpServletRequest request){
        String nickname = request.getParameter("nickname");
        String username = request.getParameter("username");
        LOG.info(nickname);
        LOG.info(username);
        model.put("nickname", nickname);
        model.put("username", username);
        model.put("navigation", navigation());  //最上部导航栏
        model.put("menu", menu());      //左侧菜单栏
    }

    /**
     * @Description: 初始化导航栏
     * @param:
     * @return:  List<MenuTree>
     * @Author: yangjun23614
     * @Date: 14:35 2018/2/6
     * V1.0
     */
    private List<MenuTree> navigation(){
        List<MenuTree> navList = new ArrayList<MenuTree>();

/*        // 基本信息导航栏
        MenuTree infoNav = new MenuTree();
        infoNav.setId("01");
        infoNav.setTitle("个人信息");
        infoNav.setUrl("/menu/info.htm");
        navList.add(infoNav);

        // 工单绩效导航栏
        MenuTree orderAndKpiNav = new MenuTree();
        orderAndKpiNav.setId("02");
        orderAndKpiNav.setTitle("工单绩效");
        orderAndKpiNav.setUrl("/menu/orderAndKpiMenu.htm");
        navList.add(orderAndKpiNav);*/

        return navList;
    }

    /**
     * 基本信息菜单
     * @return
     */
    public List<MenuTree> menu(){
        MenuTree menu;

        List<MenuTree> bigMenu = new ArrayList<MenuTree>();

        // 个人信息
        MenuTree infoMenu = new MenuTree();
        infoMenu.setTitle("个人信息");
        infoMenu.setId("10000");
        // 个人信息下的子菜单
        List<MenuTree> subInfoMenu = new ArrayList<MenuTree>();
        menu = new MenuTree();
        menu.setId("10001");
        menu.setTitle("账号信息");
        menu.setUrl("/menu/info/account.htm");
        subInfoMenu.add(menu);
        infoMenu.setItems(subInfoMenu);
        bigMenu.add(infoMenu);


        // 咨询量
        MenuTree orderKpiMenu = new MenuTree();
        orderKpiMenu.setId("20000");
        orderKpiMenu.setTitle("我的咨询");

        // 工单绩效下的子菜单
        List<MenuTree> subOrderKpiMenu = new ArrayList<MenuTree>();
        menu = new MenuTree();
        menu.setId("20001");
        menu.setTitle("最近联系人");
        menu.setUrl("/menu/consult/latestLinker.htm");
        subOrderKpiMenu.add(menu);

/*        menu = new MenuTree();
        menu.setId("20002");
        menu.setTitle("我的成长线");
        menu.setUrl("/menu/consult/growLine.htm");
        subOrderKpiMenu.add(menu);*/


        orderKpiMenu.setItems(subOrderKpiMenu);

        bigMenu.add(orderKpiMenu);


        List<MenuTree> list = new ArrayList<MenuTree>();
        menu = new MenuTree();
        menu.setId("10");
        menu.setTitle("");
        menu.setItems(bigMenu);
        list.add(menu);//菜单树封装完成

        return list;

    }

    @RequestMapping(value="/menu/info/account.htm")
    public void account(ModelMap model, HttpSession session, HttpServletRequest request){
        //String username = session.getAttribute("username"); 根据session中的username查找一切需要的数据
        Client client = new Client();
        client.setCompany("恒生电子");
        client.setDept("1");
        client.setMailbox("165982265@qq.com");
        client.setSex(1);
        client.setUsername("chenji23607");
        client.setNickname("陈济");
        client.setPhonenumber("64454844");
        model.put("client", client);
    }

    @RequestMapping(value = "/demo/chart/linebar.json")
    public @ResponseBody Map getlinebar(HttpServletRequest request) {
        String dimension = (String) request.getParameter("dimension");
        String yearStr = (String) request.getParameter("year");
        String quarterStr = (String) request.getParameter("quarter");
        String monthStr = (String) request.getParameter("month");
        String weekStr = (String) request.getParameter("week");
        String dayStr = (String) request.getParameter("day");

        String[] xData = new String[]{"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        LOG.info(dimension);

/*    	if("week".equalsIgnoreCase(dimension)) {
    		xData = new String[]{"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
    	} else if("month".equalsIgnoreCase(dimension)) {
    		xData = new String[]{"1号","2号","3号","4号","5号",
    				"1号","2号","3号","4号","5号",
    				"1号","2号","3号","4号","5号",
    				"1号","2号","3号","4号","5号",
    				"1号","2号","3号","4号","5号",
    				"1号","2号","3号","4号","5号"};
    	} else if("quarter".equalsIgnoreCase(dimension)) {
    		xData = new String[]{"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
    	} else {
    		xData = new String[]{"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
    	}*/


        Map<String,Object> result = new HashMap<String,Object>();
        List<lineDto> list = new ArrayList<lineDto>();

        int[] timeData = {135, 165, 230, 120, 100, 310, 225, 150, 130, 50, 80, 260};

        lineDto timeObj = new lineDto();
        timeObj.setName("时长");
        timeObj.setData(timeData);
        timeObj.setType("bar");
        timeObj.setyAxisIndex(0);
        list.add(timeObj);

        int[] queryCountData = {30, 47, 65, 26, 28, 70, 40, 62, 48, 18, 23, 66};
        lineDto queryCountObj = new lineDto();
        queryCountObj.setName("咨询量");
        queryCountObj.setData(queryCountData);
        queryCountObj.setType("line");
        list.add(queryCountObj);

        int[] avgTimeData = new int[12];
        for(int i=0,len=avgTimeData.length; i<len; i++) {
            avgTimeData[i] = timeData[i]/queryCountData[i];
        }
        lineDto aveTimeObj = new lineDto();
        aveTimeObj.setName("平均时长");
        aveTimeObj.setData(avgTimeData);
        aveTimeObj.setyAxisIndex(1);
        aveTimeObj.setType("bar");
        list.add(aveTimeObj);

        result.put("yData",list);
        result.put("xData", xData);
        return result;
    }

}
